from DrissionPage import ChromiumPage
from utils.parse import parseURL
import json
from tqdm import tqdm
import pandas as pd
import time
import random

def load_dict_from_json(filename):
    with open(filename, 'r', encoding='utf-8') as file:
        return json.load(file)

def save_to_excel(filename, data):
    df = pd.DataFrame(data)
    df.to_excel(filename, index=False)
    print("数据已保存到Excel文件：", filename)

if  __name__ == '__main__':

    savepath = 'data/bilibili-info.xlsx'
    page = ChromiumPage()
    page.set.load_mode.none()  # 设置加载模式为none

    # tab = page.get_tab()
    biliInfo = []

    loaded_data = load_dict_from_json('bilibili-url.json')
    for tag, info in loaded_data.items():
        # 第一层取品牌
        for label, urls in info.items():
            # 第二层取产品类型
            for url in tqdm(urls):
                time.sleep(random.uniform(0.5, 3.0))
                try:
                    tab = page.get_tab()
                    parse = parseURL(url, tab)
                    videoDetail = parse.parseData()
                    if videoDetail:
                        videoDetail['tag'] = tag
                        videoDetail['lable'] = label
                        videoDetail['url'] = url
                        print(videoDetail)
                        biliInfo.append(videoDetail)
                except Exception as e:
                    print(f"Error processing URL {url}: {e}")
                    continue

    save_to_excel(savepath, biliInfo)
    # url = 'https://www.bilibili.com/video/BV1SkZ6YUE4w/'

    # tab = page.get_tab()
    # parse = parseURL(url, tab)
    # videoDetail = parse.parseData()
    # biliInfo.append(videoDetail)
    # save_to_excel(savepath, biliInfo)
    # print(biliInfo)
    # for url in herfs:
    #     parse = parseURL(url, tab)
    #     parse.parseData()